package com.laobiao.file_netty_lib.packet.command.upload;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.laobiao.file_netty_lib.content.FileContentApplication;
import com.laobiao.file_netty_lib.exception.FileBaseException;
import com.laobiao.file_netty_lib.packet.command.BaseCommandPacket;
import com.laobiao.file_netty_lib.packet.command.CommandCirculationType;
import com.laobiao.file_netty_lib.packet.command.PacketCommandType;
import com.laobiao.file_netty_lib.util.BinaryConvertUtil;
import com.laobiao.file_netty_lib.util.CheckCodeUtil;

/**
 * 接收到的文件上传命令
 * @author 38079
 *
 */
public class FileUploadCommand extends BaseCommandPacket{
	/**
	 * 文件总长度
	 */
	private long fileSize;
	/**
	 * 文件校验码
	 */
	private byte[] checkCode;
	
	/**
	 * 上传到服务器的文件存储路径
	 */
	private String remoteFilePath;
	
	
	public FileUploadCommand(String localFilePath,String remoteFilePath,Integer businessGroup) {
		this(new File(localFilePath),remoteFilePath,businessGroup);
	}
	public FileUploadCommand(File localFile,String remoteFilePath,Integer commandGroup) {
		super();
		setCommand(PacketCommandType.UPLOAD);
		if(commandGroup!=null) {
			setBusinessGroup(commandGroup);
		}
		this.remoteFilePath=remoteFilePath;
		if(!localFile.exists() || localFile.isDirectory()) {
			throw new FileBaseException("文件不存在:"+localFile.getAbsolutePath(), FileBaseException.ERR_PARAM_WRONG);
		}
		this.fileSize=localFile.length();
		this.checkCode=CheckCodeUtil.getCheckCode(localFile);
	}
	
	/**
	 * 根据参数构建文件上传命令包
	 * @param remoteFilePath 保存到服务器的文件地址
	 * @param commandGroup 参数组
	 * @param dataSize 内容长度
	 * @param checkCode 内容校验码
	 */
	public FileUploadCommand(String remoteFilePath,Integer commandGroup,long dataSize,byte[] checkCode) {
		super();
		setCommand(PacketCommandType.UPLOAD);
		if(commandGroup!=null) {
			setBusinessGroup(commandGroup);
		}else {
			setBusinessGroup(FileContentApplication.getLastBusinessNumber());
		}
		this.remoteFilePath=remoteFilePath;
		this.fileSize=dataSize;
		this.checkCode=checkCode;
	}
	
	/**
	 * 根据命令包构建文件上传命令
	 * @param commandBytes
	 */
	public FileUploadCommand(byte[] commandBytes) {
		ByteBuffer buf = setBaseCommand(commandBytes);
		/**
		 * 文件内容长度
		 */
		byte[] chunk=new byte[8];
		buf.get(chunk);
		setFileSize(BinaryConvertUtil.bytesToLong(chunk));//设置文件内容长度
		
		/**
		 * 校验码
		 */
		chunk=new byte[20];
		buf.get(chunk);
		setCheckCode(chunk);//设置校验码
		
		/**
		 * 文件名长度
		 */
		chunk=new byte[4];
		buf.get(chunk);
		int remoteNameSize = BinaryConvertUtil.byteArrayToInt(chunk);//文件名长度
		/**
		 * 文件名
		 */
		chunk=new byte[remoteNameSize];
		buf.get(chunk);
		try {
			setRemoteFilePath(new String(chunk,"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			throw new FileBaseException("接收到的命令中文件名编码失败！", FileBaseException.ERR_PARAM_WRONG);
		}
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public byte[] getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(byte[] checkCode) {
		this.checkCode = checkCode;
	}

	public String getRemoteFilePath() {
		return remoteFilePath;
	}

	public void setRemoteFilePath(String remoteFilePath) {
		this.remoteFilePath = remoteFilePath;
	}

	/**
	 * 长度1014
	 */
	@Override
	protected byte[] getCommContentBytes() {
		ByteBuffer buf=ByteBuffer.allocate(1014);
		buf.put(BinaryConvertUtil.longToBytes(fileSize));//文件长度
		buf.put(checkCode);//校验码
		try {
			byte[] filePathBytes = remoteFilePath.getBytes("UTF-8");
			buf.put(BinaryConvertUtil.intToByteArray(filePathBytes.length));//设置文件名
			buf.put(filePathBytes);//设置文件名
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			throw new FileBaseException("文件名无法转换编码："+remoteFilePath,e, FileBaseException.ERR_PARAM_WRONG);
		}
		return buf.array();
	}


	@Override
	public CommandCirculationType circulationType() {
		// TODO Auto-generated method stub
		return CommandCirculationType.BUSINESS;
	}
	@Override
	public String toString() {
		return "FileUploadCommand [fileSize=" + fileSize + ", checkCode=" + Arrays.toString(checkCode)
				+ ", remoteFilePath=" + remoteFilePath + ", circulationType()=" + circulationType() + ", getCommand()="
				+ getCommand() + ", getBusinessGroup()=" + getBusinessGroup() + ", getPacketType()=" + getPacketType()
				+ "]";
	}
	
}
